const _function = require("../../utils/functionData.js")
Page({
  data: {
    club: {
      // id: 1,
      // logo: 'https://example.com/club-logo.jpg',
      // name: '编程爱好者社团',
      // description: '欢迎所有对编程感兴趣的同学加入！',
      // members: 120,
      // activityCount: 5,
      // isJoined: false,
      // activities: [
      //   {
      //     id: 1,
      //     cover: 'https://example.com/activity1-cover.jpg',
      //     title: 'JavaScript入门讲座',
      //     time: '2023-10-15 14:00'
      //   },
      //   {
      //     id: 2,
      //     cover: 'https://example.com/activity2-cover.jpg',
      //     title: 'React框架实战',
      //     time: '2023-10-20 15:00'
      //   }
      // ]
    },
    activitiesList: [],
    clubId: null,
    isJoin: false,
    _UrlGet_: wx.getStorageSync('img_url')
  },
  onLoad(options) {
    this.setData({
      clubId: options.id
    })
    this.getStudentSocietyList()
    this.getSocietyDetail()
  },
  getSocietyDetail() {
    _function.getSocietyDetail(this.data.clubId, '', this.getSocietyDetailBack, this)
  },
  getSocietyDetailBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      this.setData({
        club: dataAll.data
      })
      this.getSocietyActivitiesList()
    }
  },
  getSocietyActivitiesList() {
    _function.getSocietyActivitiesList(this.data.clubId, '', this.getSocietyActivitiesListBack, this)
  },
  getSocietyActivitiesListBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      this.setData({
        activitiesList: dataAll.rows
      })
    }
  },
  // 加入/退出社团
  joinClub: function () {
    if (this.data.isJoin) {
      return
    }
    const params = {
      studentId: getApp().globalData.userInfo.userId,
      societyId: this.data.clubId
    }
    _function.postStudentSociety(params, this.postStudentSocietyBack, this)
  },
  postStudentSocietyBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      this.getStudentSocietyList()
      this.getSocietyDetail()
      wx.showToast({
        title: '加入社团成功',
        icon: 'none',
        duration: 1500,
        mask: true,
        success: () => {
        }
      });
    }
  },
  getStudentSocietyList() {
    const prams = {
      clubId: this.data.clubId,
      studentId: getApp().globalData.userInfo.userId
    }
    _function.getStudentSocietyList(prams, this.getStudentSocietyListBack, this)
  },
  getStudentSocietyListBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {

      if (dataAll.rows.length > 0) {
        this.setData({
          isJoin: true
        })
      }
    }
  },
  // 跳转到活动详情
  navigateToActivityDetail: function (e) {
    const id = e.currentTarget.dataset.id; // 获取 data-id 
    wx.navigateTo({
      url: '/pages/activitydetail/activitydetail?id=' + id,
    })
  }
});